<template>
	<view>
		<u--form :rules="rules" :model="form" ref="uForm">
			<u-form-item label="名称" prop="name" borderBottom ref="item1" :customStyle="btnStyle1">
				<u--input v-model="form.nickname"></u--input>
			</u-form-item>
			<u-form-item label="手机" prop="name" borderBottom ref="item1" :customStyle="btnStyle1">
				<u--input v-model="form.mobile"></u--input>
			</u-form-item>

			<u-form-item label="密码" prop="password" borderBottom ref="item1" :customStyle="btnStyle1">
				<u--input v-model="form.password"></u--input>
			</u-form-item>
			<u-form-item label="性别" :customStyle="btnStyle2">
				<u-radio-group v-model="radiovalue1" placement="row">
					<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, index) in radiolist1" :key="item.value"
						:label="item.name" :name="item.name" @change="radioChange(item.value)">
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="状态" :customStyle="btnStyle2">
				<u-radio-group v-model="radiovalue2" placement="row">
					<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, index) in radiolist2" :key="item.value"
						:label="item.name" :name="item.name" @change="radioChange1(item.value)">
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="来源" prop="username" borderBottom @click="show = true" ref="item1"
				:customStyle="btnStyle1">
				<u--input v-model="form.source" disabled disabledColor="#ffffff" placeholder="请选择"
					suffixIcon="arrow-down"></u--input>
			</u-form-item>
			<u-form-item label="地区" prop="city" borderBottom @click="show1 = true" ref="item2" :customStyle="btnStyle1">
				<u--input v-model="form.city" disabled disabledColor="#ffffff" placeholder="请选择"
					suffixIcon="arrow-down"></u--input>
			</u-form-item>
			<u-picker :show="show" :columns="sourcelist" keyName="name" @cancel="show = false"
				@confirm="confirm"></u-picker>


			<u-picker :show="show1" @cancel="show1 = false" ref="uPicker" :defaultIndex="indexs" :columns="columns"
				@confirm="confirm1" @change="changeHandler">
			</u-picker>
			<u-button @click="submit" color="#19BC9C">提交</u-button>
			<u-toast ref="uToast"></u-toast>
		</u--form>
	</view>

</template>

<script>
	import address from '/src/components/pick-regions/regions.json'
	export default {
		data() {
			return {
				btnStyle1: {
					marginLeft: "50px",
					width: '80%',
					label: "30px"
				},
				btnStyle2: {
					width: '30%',
					marginLeft: "50px",
				},
				busid: 0,
				show: false,
				show1: false,
				addressData: {
					id: "",
					province: "",
					city: "",
					area: "",
					detail: "",
					userName: "",
					phone: "",
					isDefault: false
				}, //地址数据
				columns: [],
				province: [],
				city: [],
				area: [],
				indexs: [],
				address: address,
				form: {
					nickname: '',
					mobile: "",
					email: "",
					radiovalue1: 0,
					radiovalue2: 0,
					source: "",
					sourceid: "",
					city: "",
					code: "",
					password: ""
				},
				rules: {
					'email': {
						type: 'email',
						message: '请填写正确的邮箱',
						trigger: ['blur', 'change']
					},

				},

				LoginUser: {},
				info: {
					source: {}
				},
				radiolist1: [{
						name: '保密',
						value: 0,
						disabled: false
					},
					{
						name: '男',
						value: 1,
						disabled: false
					},
					{
						name: '女',
						value: 2,
						disabled: false
					}
				],
				radiolist2: [{
						name: '未成交',
						value: 0,
						disabled: false
					},
					{
						name: '已成交',
						value: 1,
						disabled: false
					},
				],
				radiovalue1: '保密',
				radiovalue2: '未成交',
				sourcelist: [],

			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		onLoad(options) {
			this.busid = options.busid ?? 0
			this.getSourceData()
		},
		onShow() {
			this.$u.auth.check()
			this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {};
			this.formatData();
			this.getBusiness();
		},
		methods: {
			async getBusiness() {
				let result = await this.$u.api.business.Businesslist({
					busid: this.busid
				});
				if (result.code == 1) {
					this.form = result.data
					this.radiovalue1 = result.data.gender
					this.radiovalue2 = result.data.deal

					this.form.city = result.data.province + '-' + result.data.city + '-' + result.data.district
				}
			},
			confirm({
				value
			}) {
				this.show = false
				this.form.source = value[0].name
				this.form.sourceid = value[0].id

			},
			confirm1({
				value
			}) {

				// this.form.sourceid=value[0].id

				this.form.city = value[0] + '-' + value[1] + '-' + value[2]
				this.show1 = false
			},
			async getSourceData() {
				let result = await this.$u.api.business.sourcelist();
				if (result.code == 1) {
					// result.data.forEach((item,key)=>{
					// 	this.sourcelist.push(item.name)
					// })

					this.sourcelist.push(result.data)
					// console.log(this.sourcelist)
				}
			},
			radioChange(n) {
				// console.log('radioChange', n);
				this.form.radiovalue1 = n
			},
			radioChange1(n) {
				// console.log('radioChange', n);
				this.form.radiovalue2 = n
			},
			formatData() {
				this.province = address.map(t => {
					return {
						name: t.name,
						code: t.code
					}
				})
				this.city = address.map(t => t.childs.map(v => {
					return {
						name: v.name,
						code: v.code
					}
				}))
				this.area = address.map(t => t.childs.map(v => v.childs.map(i => {
					return {
						name: i.name,
						code: i.code
					}
				})))
				// console.log('省',this.province);
				// console.log('市',this.city);
				// console.log('区',this.area);

				this.columns = [
					this.province.map(res => res.name),
					this.city[0].map(res => res.name),
					this.area[0][0].map(res => res.name)
				]
				if (this.addressData.province && this.addressData.city && this.addressData.area) {
					let pIdx = this.province.findIndex(v => v.name == this.addressData.province);
					this.columns[1] = this.city[pIdx].map(res => res.name)
					let cIdx = this.city[pIdx].findIndex(v => v.name == this.addressData.city);
					this.columns[2] = this.address[pIdx][cIdx].map(res => res.name)
					let aIdx = this.address[pIdx][cIdx].findIndex(v => v.name == this.addressData.area)
					this.indexs = [pIdx, cIdx, aIdx];
					console.log(pIdx, cIdx, aIdx);

				}
			},
			changeHandler(e) {
				const {
					columnIndex, //当前改变值的列下标
					value,
					values, //当前变化列的数组内容
					indexs, //当前地区值下标
					picker = this.$refs.uPicker
				} = e
				// console.log('value',value)
				// console.log(values)
				// console.log(e)
				// console.log(indexs)

				if (columnIndex === 0) {
					picker.setColumnValues(1, this.city[indexs[0]].map(v => v.name))
					picker.setColumnValues(2, this.area[indexs[0]][0].map(v => v.name))

				}
				if (columnIndex === 1) {
					picker.setColumnValues(2, this.area[indexs[0]][indexs[1]].map(v => v.name))
				}
				this.form.code = this.address[indexs[0]].childs[indexs[1]].childs[indexs[2]].code
				// this.form.province = this.province[index[0]]

			},
			submit() {
				this.$refs.uForm.validate().then(async (res) => {
					let result = await this.$u.api.business.PerseaEdit({
						busid: this.busid,
						nickname: this.form.nickname,
						mobile: this.form.mobile,
						gender: this.form.radiovalue1,
						deal: this.form.radiovalue2,
						sourceid: this.form.sourceid,
						password: this.form.password,
						code: this.form.code,

					})
					if (result.code == 1) {
						this.$refs.uToast.show({
							type: 'success',
							message: result.msg,
							complete: () => {
								uni.navigateTo({
									url: '/pages/business/persea/index'
								})
							}
						})
					}
					// console.log(this.form)
				}).catch(errors => {})

			}
		}
	}
</script>

<style>
</style>